package com.example.hao.ximalaya.adapter.viewholder;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.hao.ximalaya.R;
import com.example.hao.ximalaya.pojo.HotData;
import com.example.hao.ximalaya.pojo.Type26listItem;
import com.google.gson.Gson;
import com.google.gson.JsonObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class Type26ViewHolder extends RecyclerView.ViewHolder {

    private Context context;

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_right)
    ImageView ivRight;
    @BindView(R.id.tv_more)
    TextView tvMore;
    @BindView(R.id.rv_item)
    RecyclerView rvItem;
    @BindView(R.id.ll_change)
    LinearLayout llChange;
    private ItemRvAdapter itemRvAdapter;


    public Type26ViewHolder(Context context, View itemView) {
        super(itemView);
        this.context = context;
        ButterKnife.bind(this, itemView);
        initView();
    }

    private void initView() {
        rvItem.setLayoutManager(new GridLayoutManager(context, 3,LinearLayoutManager.VERTICAL, false));
        itemRvAdapter = new ItemRvAdapter(context);
        rvItem.setAdapter(itemRvAdapter);
    }

    ArrayList<Type26listItem> listItems;

    /**
     * 为 ViewHolder 绑定数据
     *
     * @param data 数据对象
     */
    public void setData(HotData.ListBeanX data) {
        List<JsonObject> list = data.getList();
        listItems = new ArrayList<>();
        for (JsonObject jsonObject : list) {
            Type26listItem type26listItem = new Gson().fromJson(jsonObject, Type26listItem.class);
            listItems.add(type26listItem);
        }
        itemRvAdapter.setListItems(listItems);
        tvTitle.setText(data.getTitle());



    }

    class ItemRvAdapter extends RecyclerView.Adapter<ItemRvAdapter.ItemViewHolder> {

        Context context;
        List<Type26listItem> listItems;


        public void setListItems(List<Type26listItem> listItems) {
            this.listItems = listItems;
            notifyDataSetChanged();
        }

        public ItemRvAdapter(Context context) {
            this.context = context;
        }


        @NonNull
        @Override
        public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View layout = LayoutInflater.from(context).inflate(R.layout.view_rv_item_type_26_rv_item, parent, false);
            return new ItemViewHolder(layout);
        }

        @Override
        public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) {
            Type26listItem type26listItem = listItems.get(position);
            Glide.with(context).load(type26listItem.getCover()).into(holder.ivIcon);
            holder.tvTitle.setText(type26listItem.getChannelName());
        }

        @Override
        public int getItemCount() {
            return listItems == null ? 0 : listItems.size();
        }

        class ItemViewHolder extends RecyclerView.ViewHolder {

            @BindView(R.id.iv_icon)
            ImageView ivIcon;
            @BindView(R.id.tv_title)
            TextView tvTitle;

            public ItemViewHolder(View itemView) {
                super(itemView);
                ButterKnife.bind(this,itemView);
            }
        }
    }


}
